@extends('layouts.header')
@section('content')
    <nav class="breadcrumb">
        <i class="Hui-iconfont">&#xe67f;</i> 首页 <span class="c-gray en">&gt;</span> 检测业务 <span class="c-gray en">&gt;</span> 上传平台 > {{request('client_name','样品列表')}}
        <a class="btn btn-primary radius r" style="line-height:1.6em;margin-top:3px" href="javascript:location.replace(location.href);" title="刷新"><i class="Hui-iconfont">&#xe68f;</i></a>
    </nav>
    <div class="page-container">
        <div>
            <b class="btn btn-primary radius " id="toThird">上传</b>
            <a class="ml-5 btn btn-primary radius" onclick="editThird()">同步</a>
            <a class="ml-5 btn btn-primary radius" onclick="download_report({{request('id')}})"><i class="Hui-iconfont">&#xe640;</i>批次检测报告</a>
            <a class="ml-5 btn btn-primary radius" onclick="download_sample_report()"><i class="Hui-iconfont">&#xe640;</i>样品报告</a>
        </div>
        <div class="mt-10">
            <table class="table table-border table-bordered table-bg table-hover table-responsive" id="datatable">
                <thead>
                    <tr class="text-c">
                        <th>
                            <input type="checkbox" id="checkAll">
                        </th>
                        <th>条形码</th>
                        <th>平台ID</th>
                        <th>样品名称</th>
                        <th>额定电压</th>
                        <th>样品状态</th>
                        <th>结论</th>
                        <th>提交时间</th>
                        <th>状态</th>
                        <th>审核时间</th>
                        <th>审核次数</th>
                        <th>审核人</th>
                        <th>试验日期</th>
                        <th>上传次数</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach($data as $item)
                        @if($item->third_id)
                        <tr class="text-c">
                            <td>
                            @if($item->res != 0)
                            <input type="checkbox" value="{{$item->id}}" data-third_id="{{$item->a_third_id}}" />
                            @endif
                            </td>
                            <td>{{$item->bar_code}}</td>
                            <td class="third_id">{{$item->third_id}}</td>
                            <td>{{$item->tool_name}}</td>
                            <td>{{$item->voltage}}</td>
                            <td>{{$item->sample_status}}</td>
                            <td>
                                <span class="badge badge-{{$status['res_class'][$item->res]}} radius">{{$status['res'][$item->res]}}</span>
                            </td>
                            <td>{{$item->submit_time}}</td>
                            <td>
                                <span class="badge badge-{{$status['status0_class'][$item->status0]}} radius">{{$status['status0'][$item->status0]}}</span>
                            </td>
                            <td>{{$item->check_time}}</td>
                            <td>{{$item->check_times}}</td>
                            <td>{{$item->audit_man}}</td>
                            <td>{{$item->test_time}}</td>
                            <td class="up_times">{{$item->up_times}}</td>
                            <td style="width:200px;">
                                <a class="ml-5 btn btn-primary radius size-MINI" onclick="layer_show('结果查看','/{{config('constant.name_admin')}}/itemData/view?id={{$item->id}}')">结果查看</a>
                                <a class="ml-5 btn btn-primary radius size-MINI" onclick="download_report({{$item->id}},1,1)"><i class="Hui-iconfont">&#xe640;</i>样品报告</a>
                                @if($item->res != 0)
                                <a class="ml-5 btn btn-primary size-MINI radius" onclick="toThird({{$item->id}},{{$item->a_third_id}})"><i class="Hui-iconfont">&#xe642;</i>上传</a>
                                @endif
                            </td>
                        </tr>
                        @endif
                    @endforeach
                </tbody>
            </table>
        </div>
    </div>

    <script type="text/javascript">
        $(function(){
            $('#datatable').DataTable({
                paging:false,
                dom:'iftr',
                language: { url: '/admin/lib/datatables/dataTables_Chinese_Lang.json' },
            })
        })
        /*上传到第三方系统*/
        function toThird(id,third_id) {
            var obj = event.target;
            //浮层
            tijiao = layer.load(2, {shade: [0.05, '#000']});
            let sample_ids = [],
                third_ids = [];
            sample_ids.push(id);
            third_ids.push(third_id);
            toThird2(sample_ids,third_ids);
        }

        /*下载原始记录*/
        function download_report(id,pdf=0,sample=0) {
            // console.log(new Date());
            window.open('/{{config('constant.name_admin')}}/exportReport/export?sample='+sample+'&toPdf='+pdf+'&id=' + id, '_blank');
        }
        function download_sample_report() {
            if ($('input:checkbox:checked','tbody').length == 0) {
                layer.msg('请先选择样品！');
                return false;
            }
            let checkedObj = $('input:checkbox:checked','tbody'),
                length = $(checkedObj).length;
            var perTime = 2000;
            layer.msg('每'+perTime/1000+'秒请求下载一个报告中...',{icon:6,time:length*perTime});
            $(checkedObj).each(function (index) {
                var id = $(this).val();
                setTimeout(function(){
                    download_report(id,1,1);
                },perTime*index);
            })
        }
        $('#toThird').on('click',function() {
            let sample_ids = [],
                third_ids = [];
            $('input:checkbox:checked','tbody').each(function(){
                third_ids.push($(this).data('third_id'));
                sample_ids.push($(this).val());
            })
            ajax('post','toThird',{sample_ids:sample_ids,third_ids:third_ids,_form_token:'{{csrf_form_token()}}'});
        })
        //同步更新第三方ID的数据
        function editThird() {
            let sample_ids = [],
                third_ids = [], //平台id
                third_id2 = []; //第三方样品id
            $('input:checkbox:checked','tbody').each(function(){
                sample_ids.push($(this).val());
                third_ids.push($(this).data('third_id'));
                third_id2.push($(this).closest('tr').find('td.third_id').text());
            })
            if (third_id2.length == 0) {
                layer.msg('请选择要更新的样品！');
                return false;
            }
            toThird2(sample_ids,third_ids,third_id2);
        }

        /*
        sample_ids:样品id，
        third_ids：第三方平台id
        third_id2：第三方样品id（同步用）
        export2：导出标识（0不导出，1导出）
        */
        function toThird2(sample_ids,third_ids,third_id2=[],export2=0) {
            let params = {
                    sample_ids:sample_ids,
                    third_ids:third_ids,
                    export:export2,
                    _form_token:'{{csrf_form_token()}}'
                };
            if (third_id2.length > 0) {
                params['type']='sync';
                params['third_id2'] = third_id2;
            }
            ajax('post','toThird',params,function (res) {
                
                if (res == 1) {
                    let msg = third_id2.length > 0 ? '同步成功！':'上传成功！';
                    layer.msg(msg,{time:2000},function () {
                        location.reload();
                    });
                }else{
                    if (res.data !== undefined && res.data.file !== undefined) {
                        layer.msg('导出中');
                        let filename = res.data.file;
                        const a = document.createElement("a");
                        a.style.display = "none";
                        a.href = '/'+filename;
                        a.download = filename;
                        a.target = "_blank";
                        // 使用target="_blank"时，添加rel="noopener noreferrer" 堵住钓鱼安全漏洞 防止新页面window指向之前的页面
                        a.rel = "noopener noreferrer";
                        document.body.append(a);
                        a.click();
                        setTimeout(() => {
                            a.remove();
                        }, 1000);
                        return;
                    }
                    layer.confirm(res,{title:'报错信息：',btn:['关闭']},function (index) {
                        layer.close(index);
                    });
                }
            },0);
        }
    </script>
@endsection